﻿<?php
session_start();
$begintime = microtime(true);
require_once './lib.php';
header("Content-type:text/html;charset=utf-8");


/**
 * @param $table 需要更新或是查询的表
 * @param $qishu 云端获取的开奖期数
 * @param $code  云端获取的开奖号码
 * @param int $parnum 根据参数选择对应的方法
 */
function insLotteryResult($table, $qishu, $code, $parnum = 5)
{

    $mysql = new DBAccess();
    if ($parnum == 5) {
        $sql = "select id from $table  WHERE qishu='$qishu'";
        $ball = explode(',', $code);
        if (!$mysql->getRow($sql)) {
            $ins_res = array(
                'qishu' => $qishu,
                'create_time' => date('Y-m-d H:i:s'),
                'datetime' => date('Y-m-d H:i:s'),
                "ball_1" => $ball[0],
                "ball_2" => $ball[1],
                "ball_3" => $ball[2],
                "ball_4" => $ball[3],
                "ball_5" => $ball[4],
            );
            $mysql->insertRow($table, $ins_res);

        }
        return $ins_res;
    } elseif ($parnum == 3) {
        $sql = "select id from $table  WHERE qishu='$qishu'";
        $ball = explode(',', $code);
        if (!$mysql->getRow($sql)) {
            $ins_res = array(
                'qishu' => $qishu,
                'create_time' => date('Y-m-d H:i:s'),
                'datetime' => date('Y-m-d H:i:s'),
                "ball_1" => $ball[0],
                "ball_2" => $ball[1],
                "ball_3" => $ball[2],
            );
            $mysql->insertRow($table, $ins_res);

        }
        return $ins_res;
    } elseif ($parnum == 10) {
        $sql = "select id from $table  WHERE qishu='$qishu'";
        $ball = explode(',', $code);
        if (!$mysql->getRow($sql)) {
            $ins_res = array(
                'qishu' => $qishu,
                'create_time' => date('Y-m-d H:i:s'),
                'datetime' => date('Y-m-d H:i:s'),
                "ball_1" => $ball[0],
                "ball_2" => $ball[1],
                "ball_3" => $ball[2],
                "ball_4" => $ball[3],
                "ball_5" => $ball[4],
                "ball_6" => $ball[5],
                "ball_7" => $ball[6],
                "ball_8" => $ball[7],
                "ball_9" => $ball[8],
                "ball_10" => $ball[9],
            );
            $mysql->insertRow($table, $ins_res);
        }
        return $ins_res;
    }
}

$typeId = array(
//    array('type' => 25, 'code' => '/lotteryJSFastThree/getBaseJSFastThree.do?lotCode=10007'), //江苏快三
//    array('type' => 26, 'code' => '/lotteryJSFastThree/getBaseJSFastThree.do?lotCode=10026'), //好运快三  即  广西快3
//    array('type' => 7, 'code' => '/ElevenFive/getElevenFiveInfo.do?lotCode=10019'), //辽宁11选5
//    array('type' => 8, 'code' => '/ElevenFive/getElevenFiveInfo.do?lotCode=10018'), //上海11选5
//    array('type' => 15, 'code' => 10012), //重庆11选5
//    array('type' => 12, 'code' => '/CQShiCai/getBaseCQShiCai.do?lotCode=10004'), //新疆时时彩
    /*$typeId2 = array(
         array('type'=>3 ,'code'=>????),  //江西时时彩
         array('type'=>2 ,'code'=>10067)   //幸运11选5	即  黑龙江11选5
         );*/
    array('type' => 1, 'code' => '/CQShiCai/getBaseCQShiCai.do?lotCode=10002'), //重庆时时彩
    //array('type' => 16, 'code' => '/ElevenFive/getElevenFiveInfo.do?lotCode=10015'), //江西多乐彩  即  江西11选5
    array('type' => 6, 'code' => '/ElevenFive/getElevenFiveInfo.do?lotCode=10006'), //广东11选5
    array('type' => 18, 'code' => '/pks/getLotteryPksInfo.do?lotCode=10001'), //北京PK拾
    array('type' => 10, 'code' => '/QuanGuoCai/getLotteryInfo1.do?&lotCode=10043'), //排列3
    array('type' => 9, 'code' => '/QuanGuoCai/getLotteryInfo1.do?&lotCode=10041'), //福彩3D
    array('type' => 36, 'code' => '/CQShiCai/getBaseCQShiCai.do?lotCode=10003') //天津时时彩
);
/**
 * 循环将数据保存到数据库对应表中
 */
foreach ($typeId as $v) {
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, 'api.api68.com' . $v['code']);
    curl_setopt($curl, CURLOPT_HEADER, 0);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 3);
    curl_setopt($curl, CURLOPT_TIMEOUT, 5);
    $data = curl_exec($curl);
    $data = json_decode(trim($data), true);
    curl_close($curl);
    $data = $data['result']['data'];
    $c_t = trim($data['preDrawIssue']); //开奖期数
    $c_r = trim($data['preDrawCode']); //开奖号码
    var_dump($data);
    if ($c_t && $c_r) {
        $type = $v['type'];
        //判断彩票类型，根据彩票类型将数据存到数据库中并做相应验证并保存到session中。
        if ($type == 1) {
            $cqresult = insLotteryResult("lottery_result_cq", $c_t, $c_r, 5);
            if ($cqresult) {
                $niuniu = b5_niuniu($cqresult['ball_1'], $cqresult['ball_2'], $cqresult['ball_3'], $cqresult['ball_4'], $cqresult['ball_5']);
                $cq[] = $cqresult['ball_1'];//第一个球
                $cq[] = $cqresult['ball_2'];//第二个球
                $cq[] = $cqresult['ball_3'];//第三个球
                $cq[] = $cqresult['ball_4'];//第四个球
                $cq[] = $cqresult['ball_5'];//第五个球
                $cqs['zhs'][] = Ssc_Auto($cq, 1);//算总和
                $cqs['zh'][] = Ssc_Auto($cq, 2);//算总和大小
                $cqs['zh'][] = Ssc_Auto($cq, 3);//算总和单双
                $cqs['zh'][] = Ssc_Auto($cq, 4);//算龙虎和
                $cqs['three'][] = Ssc_Auto($cq, 5);//算前三
                $cqs['three'][] = Ssc_Auto($cq, 6);//算中三
                $cqs['three'][] = Ssc_Auto($cq, 7);//算后三
                $cqs['niuniu'][] = $niuniu;//牛牛
                $cqs['niuniu'][] = b5_niuds($niuniu);//牛大
                $cqs['niuniu'][] = b5_niudx($niuniu);//牛小
                $CQ_arr = array(
                    'hms' => $cqs,
                    'hm' => $cq,
                    'qishu' => $cqresult['qishu'],
                );
                $_SESSION['cq_res'] = $CQ_arr;
            } else {
                $_SESSION['cq_res'] = "";
            }
        }
    }
    if ($type == 18) {
        $bjpklottery_res = insLotteryResult("lottery_result_bjpk", $c_t, $c_r, 10);
        if ($bjpklottery_res) {
            $bk[] = $bjpklottery_res['ball_1'];
            $bk[] = $bjpklottery_res['ball_2'];
            $bk[] = $bjpklottery_res['ball_3'];
            $bk[] = $bjpklottery_res['ball_4'];
            $bk[] = $bjpklottery_res['ball_5'];
            $bk[] = $bjpklottery_res['ball_6'];
            $bk[] = $bjpklottery_res['ball_7'];
            $bk[] = $bjpklottery_res['ball_8'];
            $bk[] = $bjpklottery_res['ball_9'];
            $bk[] = $bjpklottery_res['ball_10'];
            $bks['zhs'][] = Pk10_Auto_quick($bk, 1);//算冠亚总和
            $bks['zh'][] = Pk10_Auto_quick($bk, 2);//算冠亚总和大小
            $bks['zh'][] = Pk10_Auto_quick($bk, 3);//算冠亚总和单双
            $bks['lh'][] = Pk10_Auto_quick($bk, 4);//1V10龙虎
            $bks['lh'][] = Pk10_Auto_quick($bk, 5);//2V9龙虎
            $bks['lh'][] = Pk10_Auto_quick($bk, 6);//3V8龙虎
            $bks['lh'][] = Pk10_Auto_quick($bk, 7);//4V7龙虎
            $bks['lh'][] = Pk10_Auto_quick($bk, 8);//5V6龙虎

            $Bjpk_arr = array(
                'hm' => $bk,
                'hms' => $bks,
                'qishu' => $bjpklottery_res['qishu'],
            );
            $_SESSION['bjpk_res'] = $Bjpk_arr;
        } else {
            $_SESSION['bjpk_res'] = "";
        }
    }
    if ($type == 9) {
        $d3result = insLotteryResult("lottery_result_d3", $c_t, $c_r, 3);
        if ($d3result) {
            $d3[] = $d3result['ball_1'];
            $d3[] = $d3result['ball_2'];
            $d3[] = $d3result['ball_3'];
            $d3s['zhs'][] = f3D_Auto($d3, 1);//福彩3d总和
            $d3s['zh'][] = f3D_Auto($d3, 2);//福彩3d大小
            $d3s['zh'][] = f3D_Auto($d3, 3);//福彩3d单双
            $d3s['zh'][] = f3D_Auto($d3, 4);//福彩3d龙虎和
            $d3s['sl'][] = f3D_Auto($d3, 5);//福彩3d三连
            $d3s['kd'][] = f3D_Auto($d3, 6);//福彩3d跨度

            $d3_arr = array(
                'd3' => $d3,
                'd3s' => $d3s,
                'qishu' => $d3result['qishu'],
            );
            $_SESSION['d3_res'] = $d3_arr;
        } else {
            $_SESSION['d3_res'] = "";
        }

    }
    if ($type == 6) {
        $gd11result = insLotteryResult("lottery_result_gd11", $c_t, $c_r, 5);

        if ($gd11result) {
            $gd11[] = $gd11result['ball_1'];//第一个球
            $gd11[] = $gd11result['ball_2'];//第二个球
            $gd11[] = $gd11result['ball_3'];//第三个球
            $gd11[] = $gd11result['ball_4'];//第四个球
            $gd11[] = $gd11result['ball_5'];//第五个球
            $gd11s['zhs'][] = gd11x5_Auto($gd11,1);//算总和
            $gd11s['zh'][] = gd11x5_Auto($gd11, 2);//算总和大小
            $gd11s['zh'][] = gd11x5_Auto($gd11, 3);//算总和单双
            $gd11s['zh'][] = gd11x5_Auto($gd11, 4);//算龙虎和
            $gd11s['three'][] = gd11x5_Auto($gd11, 5);//算前三
            $gd11s['three'][] = gd11x5_Auto($gd11, 6);//算中三
            $gd11s['three'][] = gd11x5_Auto($gd11, 7);//算后三
            $gd11_arr = array(
                'gd11' => $gd11,
                'gd11s' => $gd11s,
                'qishu' => $gd11result['qishu'],
            );
            $_SESSION['gd11_res'] = $gd11_arr;
        } else {
            $_SESSION['gd11_res'] = "";
        }

    }

}

var_dump($_SESSION);


//if ($win) {
//    $sql = "update order_lottery set status='1' WHERE order_num=$order_num and status=0";
//    $sql_money = "update user_list set money=money+$win WHERE user_id=(SELECT user_id FROM order_lottery WHERE order_num=$order_num)";
//    echo $sql_money . "<br>";
//    $mysql->update($sql);
//    $mysql->update($sql_money);
// if ($type == 16) {
//insLotteryResult("lottery_result_jx", $c_t, $c_r, 5);
//}
//if ($type == 10) {
//    insLotteryResult("lottery_result_p3", $c_t, $c_r, 3);
//
//}
//if ($type == 9) {
//    insLotteryResult("lottery_result_d3", $c_t, $c_r, 3);
//
//}
//if ($type == 36) {
//    insLotteryResult("lottery_result_tj", $c_t, $c_r, 5);
//
//}
//}
$endtime = microtime(true);
$resultime = $endtime - $begintime;
echo "页面运行时间：$resultime";
